<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="活动名称" prop="eventName">
        <el-input
          v-model="queryParams.eventName"
          placeholder="请输入活动名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户名" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入用户名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="评论状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择评论状态" clearable>
          <el-option
            v-for="dict in ich_event_comment"
            :key="dict.value"
            :label="dict.label"
            :value="Number(dict.value)"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="评论人" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入评论人"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="评论时间" prop="createTime">
        <el-date-picker clearable
          v-model="queryParams.createTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择评论时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['management:EventComment:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="openAudit"
          v-hasPermi="['management:EventComment:edit']"
        >审核</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['management:EventComment:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['management:EventComment:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="EventCommentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="评论ID" align="center" prop="commentId" />
      <el-table-column label="活动名称" align="center" prop="eventName" />
      <el-table-column label="用户名" align="center" prop="username" />
      <el-table-column
        label="评论内容"
        align="center"
        prop="content"
        :show-overflow-tooltip="true"
      >
        <template #default="{ row }">
          <span :class="!row.content ? 'text-muted' : ''">
            {{ row.content || '—' }}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        label="父评论内容"
        align="center"
        prop="parentContent"
        :show-overflow-tooltip="true"
      >
        <template #default="{ row }">
          <span :class="!row.parentContent ? 'text-muted' : ''">
            {{ row.parentContent || '—' }}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        label="顶级评论内容"
        align="center"
        prop="topContent"
        :show-overflow-tooltip="true"
      >
        <template #default="{ row }">
          <span :class="!row.topContent ? 'text-muted' : ''">
            {{ row.topContent || '—' }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="评论状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="ich_event_comment" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="评论时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="openAudit(scope.row)" v-hasPermi="['management:EventComment:edit']">审核</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['management:EventComment:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改评论管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="EventCommentRef" :model="form" :rules="rules" label-width="80px">
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 审核评论状态对话框 -->
    <el-dialog title="审核评论" v-model="auditOpen" width="520px" append-to-body>
      <el-form ref="AuditRef" :model="auditForm" :rules="auditRules" label-width="90px">


        <el-form-item label="评论状态" prop="status">
          <el-select v-model="auditForm.status" placeholder="请选择评论状态" clearable>
            <el-option
              v-for="dict in ich_event_comment"
              :key="dict.value"
              :label="dict.label"
              :value="Number(dict.value)"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="审核意见" prop="remark">
          <el-input
            v-model="auditForm.remark"
            type="textarea"
            :rows="4"
            placeholder="请输入审核意见"
            maxlength="500"
            show-word-limit
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitAudit">确 定</el-button>
          <el-button @click="cancelAudit">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="EventComment">
import { listEventComment, getEventComment, delEventComment, addEventComment, updateEventComment } from "@/api/management/EventComment"

const { proxy } = getCurrentInstance()
const { ich_event_comment } = proxy.useDict('ich_event_comment')

const EventCommentList = ref([])
const open = ref(false)
const auditOpen = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    eventName: null,
    username: null,
    status: null,
    createBy: null,
    createTime: null,
  },
  rules: {
  },
  auditForm: {
    commentId: null,
    status: null,
    remark: ''
  },
  auditRules: {
    status: [{ required: true, message: '请选择评论状态', trigger: 'change' }],
    remark: [{ required: false, message: '请输入审核意见', trigger: 'blur' }]
  }
})

const { queryParams, form, rules, auditForm, auditRules } = toRefs(data)

/** 查询评论管理列表 */
function getList() {
  loading.value = true
  listEventComment(queryParams.value).then(response => {
    EventCommentList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    commentId: null,
    eventName: null,
    username: null,
    parentId: null,
    topId: null,
    content: null,
    status: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null,
    parentContent: null
  }
  proxy.resetForm("EventCommentRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.commentId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加评论管理"
}

/** 修改按钮操作 */
function openAudit(row) {
  // 支持两种触发来源：行内按钮传 row；工具栏按钮不传 row，使用选中 ids
  const id = row?.commentId ?? (ids.value.length === 1 ? ids.value[0] : null)
  if (!id) {
    proxy.$modal.msgWarning('请先选择一条评论')
    return
  }
  auditForm.value.commentId = id
  // 若有行数据，用行状态/备注；否则保持上次或置空
  auditForm.value.status = row?.status ?? auditForm.value.status ?? null
  auditForm.value.remark = row?.remark ?? auditForm.value.remark ?? ''
  auditOpen.value = true
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["EventCommentRef"].validate(valid => {
    if (valid) {
      if (form.value.commentId != null) {
        updateEventComment(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addEventComment(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 审核提交 */
function submitAudit() {
  proxy.$refs['AuditRef'].validate(valid => {
    if (!valid) return
    const payload = {
      commentId: auditForm.value.commentId,
      status: auditForm.value.status,
      remark: auditForm.value.remark
    }
    updateEventComment(payload).then(() => {
      proxy.$modal.msgSuccess('审核成功')
      auditOpen.value = false
      getList()
    }).catch(() => {
      proxy.$modal.msgError('审核失败，请稍后重试')
    })
  })
}

/** 取消审核 */
function cancelAudit() {
  auditOpen.value = false
  auditForm.value = { commentId: null, status: null, remark: '' }
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _commentIds = row.commentId || ids.value
  proxy.$modal.confirm('是否确认删除评论管理编号为"' + _commentIds + '"的数据项？').then(function() {
    return delEventComment(_commentIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('management/EventComment/export', {
    ...queryParams.value
  }, `EventComment_${new Date().getTime()}.xlsx`)
}

getList()
</script>

<style scoped>
.text-muted {
  color: var(--el-text-color-placeholder);
}
</style>
